package com.client;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;

/**
 * Created by YQR on 2017/27/11.
 */

public class UnirestClient {
	private static Log log=LogFactory.getLog(UnirestClient.class);	
	private static String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36";
	private static HttpResponse<String> response;
	
	/*
	 * sendGet
	 * @param url
	 */
public static String sendGet(String url) {
	
		
		HttpResponse<String> response;
        try {
            response = Unirest.get(url)
            		.header("User-Agent", USER_AGENT)
            		.asString();             
            return response.getBody();          
        } catch (UnirestException e) {   
    	
            log.error("HTTP Get Error:{}",e);
        }
        return null;
    }

/*
 * sendPost
 * @param url
 * @param bodyParams
 * @param contentType
 */
public static String sendPost(String url,String contentType,String bodyParams){
	log.info("--------------------------url-------------");
	log.info(url);
	try {			
		response = Unirest.post(url)
				.header("User-Agent", USER_AGENT)
				.header("Connection","Keep-Alive")
				.header("Content-Type", contentType)
				.header("token", "erp")
				.body(bodyParams)
				.asString();
		//log response
		log.info("--------------------------response-------------");
		String Result=response.getBody();
		log.info(Result);
		return Result;
	} catch (UnirestException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		log.error("HTTP post Error:{}",e);
	}
	return null;
				
}

}
